﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace server.app.table
{
    public class Backpack : Table<comm.objst.TableBackpack>
    {
        public Backpack(World world)
            : base(Conf.TABLE_NAME_PREFIX + typeof(Backpack).Name, world)
        {
        }

        public bool CreateBackpack(comm.objst.TableBackpack tablePack)
        {
            return _world.DataManager.Call<bool>(sqlx =>
            {
                sqlx.BeginTxn();
                var r = CreateBackpack(sqlx, tablePack);
                if (r)
                {
                    sqlx.CommitTxn();
                }
                return r;
            });
        }
        public bool CreateBackpack(SQLx sqlx, comm.objst.TableBackpack tablePack)
        {
            return Orm.Insert(sqlx, tablePack);
        }

        public comm.objst.TableBackpack[] GetAllBackpack(string playerId)
        {
            return _world.DataManager.Call<comm.objst.TableBackpack[]>(sqlx =>
            {
                return GetAllBackpack(sqlx, playerId);
            });
        }
        public comm.objst.TableBackpack[] GetAllBackpack(SQLx sqlx, string playerId)
        {
            return Orm.GetList(sqlx, "select * from [" + _tableName + "] where [" + comm.objst.TableBackpack.PLAYER_ID + "]=|0|", playerId);
        }

        public comm.objst.TableBackpack GetBackpack(string backpackId)
        {
            return _world.DataManager.Call<comm.objst.TableBackpack>(sqlx =>
            {
                return GetBackpack(sqlx, backpackId);
            });
        }
        public comm.objst.TableBackpack GetBackpack(SQLx sqlx, string backpackId)
        {
            return Orm.GetObjectByKey(sqlx, backpackId);
        }

        public comm.objst.TableBackpack GetBackpackByName(string playerId, string name)
        {
            return _world.DataManager.Call<comm.objst.TableBackpack>(sqlx =>
            {
                return GetBackpackByName(sqlx, playerId, name);
            });
        }
        public comm.objst.TableBackpack GetBackpackByName(SQLx sqlx, string playerId, string name)
        {
            return Orm.GetObject(sqlx, "select * from [" + _tableName + "] whrere [" + comm.objst.TableBackpack.PLAYER_ID + "]=|0| and [" + comm.objst.TableBackpack.NAME + "]=|1|", playerId, name);
        }

        public string AddItem(SQLx sqlx, string playerId, string[] itemIds)
        {
            var allPack = GetAllBackpack(sqlx, playerId);
            var chPack = new List<comm.objst.TableBackpack>();
            var count = 0;

            if (itemIds.Length > 0)
            {
                foreach (var pack in allPack)
                {
                    var c = count;
                    for (int pos = 0; pos < pack.Max; pos++)
                    {
                        if (!pack.ItemDic.ContainsKey(pos))
                        {
                            pack.ItemDic.Add(pos, new comm.objst.BackpackItem { ItemId = itemIds[count] });
                            count++;
                            if (count == itemIds.Length)
                            {
                                break;
                            }
                        }
                    }
                    if (count != c)
                    {
                        chPack.Add(pack);
                    }
                    if (count == itemIds.Length)
                    {
                        break;
                    }
                }
            }

            if (count != itemIds.Length)
            {
                return "all Backpack not >" + itemIds.Length + " empty!";
            }
            foreach (var pack in chPack)
            {
                if (!Orm.Update(sqlx, pack))
                {
                    return comm.CodeStr.ERROR;
                }
            }
            return comm.CodeStr.OK;
        }
    }
}
